package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.utils.MediaLoadManager;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.AddVideoAdapter;
import flc.ast.bean.MyAlbumBean;
import flc.ast.databinding.ActivityAddVideoBinding;
import flc.ast.utils.AppSpUtils;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.FileUtil;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.StkPermissionHelper;

public class AddVideoActivity extends BaseAc<ActivityAddVideoBinding> {

    private AddVideoAdapter videoAdapter;
    private List<SelectMediaEntity> listSel = new ArrayList<>();
    private boolean isAllSel = false;

    @Override
    protected int onCreate() {
        return R.layout.activity_add_video;
    }

    @Override
    protected void initView() {
        mDataBinding.ivAddVideoBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvConfirm.setOnClickListener(this);
        mDataBinding.llAllSel.setOnClickListener(this);

        mDataBinding.rvVideoList.setLayoutManager(new LinearLayoutManager(mContext));
        videoAdapter = new AddVideoAdapter();
        mDataBinding.rvVideoList.setAdapter(videoAdapter);
        videoAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvConfirm://添加
                if (listSel.size() == 0) {
                    ToastUtils.showShort(R.string.please_sel_video);
                    return;
                }
                saveToAlbum();
                break;
            case R.id.llAllSel://全选
                listSel.clear();
                if (isAllSel) {
                    isAllSel = false;
                    mDataBinding.ivAllSel.setImageResource(R.drawable.aquanxuan);
                    for (SelectMediaEntity validDatum : videoAdapter.getValidData()) {
                        validDatum.setChecked(false);
                    }
                } else {
                    isAllSel = true;
                    mDataBinding.ivAllSel.setImageResource(R.drawable.ayiquanx);
                    for (SelectMediaEntity validDatum : videoAdapter.getValidData()) {
                        listSel.add(validDatum);
                        validDatum.setChecked(true);
                    }
                }
                videoAdapter.notifyDataSetChanged();
                mDataBinding.tvConfirm.setText(getString(R.string.add_num, listSel.size()));
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (videoAdapter.getItem(position).isChecked()) {
            videoAdapter.getItem(position).setChecked(false);
            listSel.remove(videoAdapter.getItem(position));
        } else {
            listSel.add(videoAdapter.getItem(position));
            videoAdapter.getItem(position).setChecked(true);
        }
        videoAdapter.notifyItemChanged(position);
        mDataBinding.tvConfirm.setText(getString(R.string.add_num, listSel.size()));
    }

    @Override
    protected void initData() {
        getPermission();
        mDataBinding.tvConfirm.setText(getString(R.string.add_num, 0));
    }

    private void getPermission() {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                .reqPermissionDesc(getString(R.string.get_video_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getData();
                    }

                    @Override
                    public void onDenied(boolean needGoSettings) {
                        mDataBinding.rvVideoList.setVisibility(View.GONE);
                    }
                }).request();
    }

    //获取视频和图片数据
    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.VIDEO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    videoAdapter.setList(selectMediaEntities);
                    mDataBinding.rvVideoList.setVisibility(View.VISIBLE);
                } else {
                    mDataBinding.rvVideoList.setVisibility(View.GONE);
                }
            }
        });
    }

    //保存选择文件到相册
    private void saveToAlbum() {
        showDialog(getString(R.string.add_ing));
        RxUtil.create(new RxUtil.Callback<String>() {

            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                List<MyAlbumBean> listSp = AppSpUtils.getAlbumList();
                if (listSp != null && listSp.size() > 0) {
                    for (MyAlbumBean bean : listSp) {
                        if (bean.getId().equals(VideoAlbumActivity.albumId)) {
                            if (bean.getList() != null && bean.getList().size() > 0) {
                                bean.getList().addAll(listSel);
                            } else {
                                bean.setList(listSel);
                            }
                            break;
                        }
                    }
                    AppSpUtils.saveAlbumList(listSp);
                }
                emitter.onNext("");
            }

            @Override
            public void accept(String path) {
                dismissDialog();
                finish();
            }
        });
    }

}